<?php

//Load Master Page
require_once('../shared/layout.php');

//Load Shared Objects & Controls
require_once('../shared/alert.php');
require_once('../shared/confirmation.php');

class Index extends Layout {

    protected $objAlert, $dlgConfirmation;
    protected $dtgProject;
    protected $pxyDelete;

    protected function Form_Create() {
        parent::Form_Create();

        $this->strContextMenu = __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/project/menu/index.tpl.php';

        $this->objAlert = new Alert($this);

        $this->dlgConfirmation = new Confirmation($this, null, new QAjaxAction('dlgConfirmation_ConfirmAction'), null);

        $this->pxyDelete = new QControlProxy($this);
        $this->pxyDelete->AddAction(new QClickEvent(), new QAjaxAction('pxyDelete_Click'));

        $this->dtgProject = new ProjectDataGrid($this);
        $this->dtgProject->ShowFilter = false;
        $this->dtgProject->ShowFooter = true;

        $this->dtgProject->Paginator = $this->dtgProject->PaginatorAlternate = new QPaginator($this->dtgProject);
        $this->dtgProject->ItemsPerPage = 10;

        $this->dtgProject->MetaAddColumn(QQN::Project()->Name);
        $this->dtgProject->MetaAddColumn(QQN::Project()->Date);
        $this->dtgProject->AddColumn(new QDataGridColumn('Applied Services', '<?= $_FORM->dtgProject_AppliedServicesColumnRender($_ITEM); ?>', 'HtmlEntities=false', 'Width=35%', array('OrderByClause' => QQ::OrderBy(QQN::Project()->ProjectService->Service->Name), 'ReverseOrderByClause' => QQ::OrderBy(QQN::Project()->ProjectService->Service->Name, false))));
        $this->dtgProject->AddColumn(new QDataGridColumn('Is Enable', '<?= $_FORM->dtgProject_IsEnableColumnRender($_ITEM); ?>', 'HtmlEntities=false', array('OrderByClause' => QQ::OrderBy(QQN::Project()->IsEnable), 'ReverseOrderByClause' => QQ::OrderBy(QQN::Project()->IsEnable, false))));
        $this->dtgProject->MetaAddEditLinkColumn('edit.php', 'Edit', 'text-info', null, QMetaControlArgumentType::QueryString);
        $this->dtgProject->MetaAddEditLinkColumn('../project_gallery', 'Gallery', 'text-important', null, QMetaControlArgumentType::QueryString);
        $this->dtgProject->MetaAddEditProxyColumn($this->pxyDelete, 'Delete', 'text-error', null);
    }

    public function dtgProject_AppliedServicesColumnRender(Project $objProject) {

        $objServiceTagArray = array();

        try {
            foreach ($objProject->GetProjectServiceArray() as $objProjectService) {
                $objServiceTagArray[] = sprintf('<span class="badge badge-success">%s</span>', $objProjectService->Service->Name);
            }
        } catch (QCallerException $objExc) {
            $objExc->IncrementOffset();
            throw $objExc;
        }

        $strToReturn = implode(' ', $objServiceTagArray);
        return $strToReturn;
    }

    public function dtgProject_IsEnableColumnRender(Project $objProject) {

        if ($objProject->IsEnable)
            return '<i class="icon-ok"></i>';

        return null;
    }

    public function pxyDelete_Click($strFormId, $strControlId, $strParameter) {

        $objProject = Project::Load($strParameter);

        $this->dlgConfirmation->strTitle = QApplication::Translate('Delete Project');
        $this->dlgConfirmation->strMessage = QApplication::Translate(sprintf('Do you want to delete "%s"?', $objProject->Name));
        $this->dlgConfirmation->btnConfirm->ActionParameter = $objProject->Id;
        $this->dlgConfirmation->Show();
    }

    public function dlgConfirmation_ConfirmAction($strFormId, $strControlId, $strParameter) {

        try {
            $objProject = Project::Load($strParameter);
            $objProject->DeleteAllProjectGalleries();
            $objProject->DeleteAllProjectServices();
            $objProject->Delete();

            $this->dtgProject->Refresh();
            $this->objAlert->Success(QApplication::Translate('Success'), QApplication::Translate(sprintf('Project "%s" Deleted Successful', $objProject->Name)));
            $this->dlgConfirmation->Hide();
        } catch (QCallerException $objExc) {
            $this->objAlert->Error(QApplication::Translate('Ooops!'), $objExc->getMessage());
        }
    }

}

Index::Run('Index', __DOCROOT__ . __SUBDIRECTORY__ . '/admin/view/project/index.tpl.php');
?>